package graphics;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import controller.Constants;

public class Oval extends RectBoundedShape {
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("oval:");
		sb.append(getLineColor().getRGB()+":");
		sb.append((isFilled()?"1":"0") + ":");
		sb.append(getFilledColor().getRGB()+":");
		sb.append(getStroke().getLineWidth()+":");
		sb.append(getStartPoint().x+","+getStartPoint().y+":");
		sb.append(getEndPoint().x+","+getEndPoint().y);
		return sb.toString();
	}

	@Override
	public void fromString(String str) {
		try{
		String[] data = str.split(":");
		if(!data[0].equals("oval"))return;
		setLineColor(new Color(Integer.parseInt(data[1])));
		setFilled(data[2].equals("1"));
		setFilledColor(new Color(Integer.parseInt(data[3])));
		setStroke(new BasicStroke(Float.parseFloat(data[4]),BasicStroke.CAP_ROUND,BasicStroke.CAP_ROUND));
		String[] point = data[5].split(",");
		setStartPoint(new Point(Integer.parseInt(point[0]),Integer.parseInt(point[1])));
		point = data[6].split(",");
		setEndPoint(new Point(Integer.parseInt(point[0]),Integer.parseInt(point[1])));
		}catch(Exception e)
		{
			if(Constants.DEBUG)e.printStackTrace();
		}
	}
	@Override
	/**
	 * Draw a oval
	 */
	public void draw(Graphics g) {
		super.draw(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setStroke(getStroke());
		g2d.setColor(getLineColor());
		g2d.drawOval(x, y, width, height);

		if (isFilled()) {
			g2d.setColor(getFilledColor());
			int strokeLineWidth = (int) getStroke().getLineWidth();
			int strokeSize = 0;
			if (strokeLineWidth % 2 != 0) {
				strokeSize = strokeLineWidth / 2;
				g2d.fillOval(x + strokeSize + 1, y + strokeSize + 1, width
						- strokeLineWidth, height - strokeLineWidth);
			} else {
				strokeSize = strokeLineWidth / 2;
				g2d.fillOval(x + strokeSize, y + strokeSize, width
						- strokeLineWidth, height - strokeLineWidth);
			}
		}
	}
	public static void main(String[] args) {
		Oval c = new Oval();
		c.setStartPoint(new Point(12,34));
		c.setEndPoint(new Point(56,78));
		System.out.println(c);
		
		Oval c2 = new Oval();
		c2.fromString(c.toString());
		System.out.println(c2);
	}
}
